<template>

    <div>
        <el-upload
                class="avatar-uploader"
                :http-request="myupload"
                action
                :auto-upload="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
                name="file"
                ref="my"
                limit="1"
        >
            <img id="showHead"  v-if="imageUrl" :src="imageUrl" class="avatar">
            <i  v-else class="el-icon-plus avatar-uploader-icon"></i>

        </el-upload>
        <div slot="tip" class="el-upload__tip">上传视频封面图片</div>
        <div slot="tip" class="el-upload__tip">只能上传jpg文件，且不超过2m</div>
    </div>
</template>

<script>
    import {request} from "../../network/request";

    export default {
        name: "submitImg",
        data() {
            return {
                imageUrl: '',
                Existence:false
            };
        },
        methods: {
            remind(){
                alert();
            },
            handleAvatarSuccess(res, file) {
                // alert();
                console.log(this.imageUrl);
                this.imageUrl = URL.createObjectURL(file.raw);
            },
            beforeAvatarUpload(file) {
                //  alert();
                if(this.existence){
                    this.$message.error('不能重复上传');
                    return false;
                }
                this.existence = true;
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            uploading(){
                this.$refs.my.submit()
            },
            myupload(content){
                //console.log(content.file)
                let myformData = new FormData()
                myformData.append('file',content.file);
                request({
                    url:"/justUplodImg",
                    method: "post",
                    data:myformData

                }).then(res=>{
                    console.log(res.data);
                    sessionStorage.setItem("videoImgUrl", res.data);
                    this.imageUrl = res.data;
                }).catch(err=>{
                    //alert(err);
                    console.log(err);
                });

            }
        },
        mounted() {
            document.getElementsByClassName(".el-upload")[0].style.border = "1px dashed #d9d9d9";
        }
    }
</script>

<style scoped>
    .avatar-uploader {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        background-color: #fff;
        width: 180px;
    }
    .avatar-uploader:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>